package com.newcapec.erp.ui;

import com.newcapec.erp.bean.WarehouseEntity;
import com.newcapec.erp.main.DataInit;
import com.newcapec.erp.utils.XMLFReader;
import com.newcapec.erp.utils.XMLFWriter;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class AddWarehouseUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;


	/**
	 * Create the frame.
	 */
	public AddWarehouseUI() {
		setIconImage(Toolkit.getDefaultToolkit().getImage("images/logo.png"));
		setTitle("新增仓库");
		setBounds(100, 100, 511, 495);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("请输入"+DataInit.warehouseColumnNames.get(0)+"：");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setBounds(110, 21, 107, 33);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("请输入"+DataInit.warehouseColumnNames.get(1)+"：");
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setBounds(99, 75, 107, 33);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("请输入"+DataInit.warehouseColumnNames.get(2)+"：");
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setBounds(67, 133, 139, 33);
		contentPane.add(label_2);
		
		textField = new JTextField();
		textField.setBounds(226, 24, 183, 27);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(226, 78, 183, 27);
		contentPane.add(textField_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(226, 136, 183, 27);
		contentPane.add(textField_2);
		
		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = textField.getText();
				String text1 = textField_1.getText();
				String text2 = textField_2.getText();
				boolean isFlag = false;
				if(text.equals("")){
					JOptionPane.showMessageDialog(null, DataInit.warehouseColumnNames.get(0)+"不能为空", "提示",JOptionPane.WARNING_MESSAGE);
					isFlag = true;
				}
				if(text1.equals("")){
					JOptionPane.showMessageDialog(null, DataInit.warehouseColumnNames.get(1)+"不能为空", "提示",JOptionPane.WARNING_MESSAGE);
					isFlag = true;
				}
				if(text2.equals("")){
					JOptionPane.showMessageDialog(null, DataInit.warehouseColumnNames.get(2)+"不能为空", "提示",JOptionPane.WARNING_MESSAGE);
					isFlag = true;
				}
				if(isFlag){
					return;
				}
				WarehouseEntity warehouseEntity=new WarehouseEntity(text,text1,text2);
				XMLFWriter.addWarehouse(warehouseEntity);
				JOptionPane.showMessageDialog(null, "恭喜你，添加成功.", "提示",JOptionPane.PLAIN_MESSAGE);
				Vector<Vector<String>> data = XMLFReader.getData(DataInit.TYPE_WAREHOUSE);
				MainUI.updateMainUI(DataInit.TYPE_WAREHOUSE,data,1);
				dispose();
			}
		});
		button.setBounds(226, 379, 93, 23);
		contentPane.add(button);
		setLocationRelativeTo(null);
		setVisible(true);
	}
}
